package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.Setmeal;
import com.itheima.service.OrderService;
import com.itheima.utils.SMSUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(path = "/order")
public class OrderController {

    @Autowired
    private JedisPool jedisPool;

    @Reference
    private OrderService orderService;

    /**
     * 提交预约信息   ---- 会员信息与预约信息
     *
     * @param map
     * @return
     */
    @PostMapping(path = "/submit")
    public Result submit(@RequestBody Map<String, String> map) {
        try {
            //1. 校验验证码是否正确
            //获取redis中的验证码
            String redis_code = jedisPool.getResource().get(map.get("telephone") + RedisConstant.SENDTYPE_ORDER);
            //获取参数中的验证码
            String code = map.get("validateCode");
            //校验验证码是否正确
            //校验不通过 , 返货错误信息
            if (!StringUtils.equalsIgnoreCase(redis_code, code)) {
                return new Result(false, MessageConstant.VALIDATECODE_ERROR);
            }
            //2. 校验通过 , 调用服务提交预约数据提交
            //设置预约方式
            map.put("orderType", Order.ORDERTYPE_WEIXIN);
            //提交预约数据
            Result result = orderService.order(map);
            if (result.isFlag()) {
                //发送短信提醒
                SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE, map.get("telephone"), map.get("orderDate"));
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SERVER_BUSY);
        }
    }

    /**
     * 查询订单详情
     *
     * @param id
     * @return
     */
    @GetMapping(path = "/{id}")
    public Result findById(@PathVariable("id") Integer id) {
        try {
            Order order = orderService.findById(id);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, order);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
    }
    @GetMapping("/findOrder")
    public Result findOrder(HttpServletRequest request, HttpServletResponse response){
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            String name = cookie.getName();
            if ("login_member_telephone".equalsIgnoreCase(name)){
                String telephone = cookie.getValue();
                List<Map<String, Object>> orderList = orderService.findByTelephone(telephone);
                return new Result(true,"查询套餐成功",orderList);
            }
        }
        return new Result(false,"查询套餐失败");
    }
    @GetMapping("/findOrderByTime")
    public Result findOrderByTime(String startTime,String endTime,HttpServletRequest request, HttpServletResponse response){
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            String name = cookie.getName();
            if ("login_member_telephone".equalsIgnoreCase(name)){
                String telephone = cookie.getValue();
                List<Map<String, Object>> orderList = orderService.findByTelephoneByTime(telephone,startTime,endTime);
                return new Result(true,"查询套餐成功",orderList);
            }
        }
        return new Result(false,"查询套餐失败");
    }
    @GetMapping("/findSetmealByOrderId")
    public Result findSetmealByOrderId(Integer id){
        try {
            Setmeal setmeal = orderService.findSetmealByOrderId(id);
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
        }catch(Exception e){
            e.printStackTrace();
            return new Result(true,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }
    @GetMapping("/findOrderInfoByOrderId")
    public Result findOrderInfoByOrderId(Integer id){
        try {
            Map<String, Object> orderInfo = orderService.findOrderInfoByOrderId(id);
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,orderInfo);
        }catch(Exception e){
            e.printStackTrace();
            return new Result(true,MessageConstant.QUERY_ORDER_FAIL);
        }
    }
}
